
module Schnell
  class Browser
    include Schnell::Container
    
    #set the proxy server and port if you have one.
    def set_proxy(proxyHost = nil,proxyPort = nil)
      @proxyHost = proxyHost
      @proxyPort = proxyPort
      return [@ProxyHost,@proxyPort]
    end

    #navigate to a url
    def goto(url)
      @driver = HtmlUnitDriver.new
      @driver.setJavascriptEnabled(true)
      @driver.setProxy(@proxyHost,@proxyPort) unless @proxyHost.nil?
      @driver.get(url)
      begin
        self.url
      rescue
        raise NavigationException
      end
      return self
    end

    #returns the title of the current page
    def title
      return @driver.getTitle
    end

    #return the text in the body of the current page
    def text
      return @driver.getText
    end

    #returns url of the current page
    def url
      return @driver.getCurrentUrl
    end
    
    #returns to the previous page in the history
    def back
      @driver.navigation.back
      return self
    end
    
    #Goes forward to the next page in the history
    def forward
      @driver.navigation.forward
      return self
    end

    protected
    #returns the current page
    def driver
      return @driver unless @driver.nil?
    end
  end
end
